package com.jxc.action.hygl;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.aop.LogInfoAop;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VMember;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VSalelist;
import com.jxc.model.vo.VUser;
import com.jxc.service.MemberServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/hygl")
@Action(value = "memberAction", results = {
		@Result(name = "member", location = "/member/member.jsp"),
		@Result(name = "memberTotal", location = "/member/memberTotal.jsp"),
		@Result(name = "memberAdd", location = "/member/memberAdd.jsp"),
		@Result(name = "memberEdit", location = "/member/memberEdit.jsp"),
		@Result(name = "saleRecord", location = "/member/saleRecord.jsp"),
		@Result(name = "pointRecord", location = "/member/pointRecord.jsp") })
public class MemberAction extends BaseAction implements ModelDriven<VMember> {
	private final static Logger logger = Logger.getLogger(MemberAction.class);
	@Autowired
	private MemberServiceI memberService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	private VMember model = new VMember();

	@Override
	public VMember getModel() {
		return model;
	}

	public void setModel(VMember model) {
		this.model = model;
	}

	public String member() {
		getSubAuths("HYGL_HYGL");
		return "member";
	}
	
	public String memberTotal() {
		getSubAuths("SJTJ_HYTJ");
		return "memberTotal";
	}

	public String memberAdd() {
		return "memberAdd";
	}

	public String memberEdit() {
		return "memberEdit";
	}
	
	public void noAuth_combobox(){
		super.writeJson(this.memberService.combobox(model));
	}

	public void add() {
		Json j = new Json();
		try {
			model.setName(new URLDecoder().decode(model.getName(),"UTF-8"));
			model.setRemark(new URLDecoder().decode(model.getRemark(),"UTF-8"));
			model.setAddress(new URLDecoder().decode(model.getAddress(),"UTF-8"));
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
					.getRequest().getSession().getAttribute(
							ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() == null) {
				model.setUid(vuser.getId());
			} else {
				VRepertory vRepertory = repertoryService.get(vuser.getReid());
				model.setUid(vRepertory.getUid());
			}
			memberService.save(model);
			j.setObj(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	@LogInfoAop(info = "编辑会员")
	public void edit() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
					.getRequest().getSession().getAttribute(
							ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() == null) {
				model.setUid(vuser.getId());
			} else {
				VRepertory vRepertory = repertoryService.get(vuser.getReid());
				model.setUid(vRepertory.getUid());
			}
			memberService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		memberService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() == null) {
			model.setUid(vuser.getId());
		} else {
			VRepertory vRepertory = repertoryService.get(vuser.getReid());
			model.setUid(vRepertory.getUid());
		}
		super.writeJson(memberService.datagrid(model));
	}

	public void datagridTotal() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() == null) {
			model.setUid(vuser.getId());
		} else {
			VRepertory vRepertory = repertoryService.get(vuser.getReid());
			model.setUid(vRepertory.getUid());
		}
		super.writeJson(memberService.datagridTotal(model));
		
		/*if (model.getRid() != null) {
			this.writeJson(memberService.datagridTotal(model));
		} else {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());		
			
			if (vuser.getReid() != null) {
				model.setRid(vuser.getReid());
				this.writeJson(memberService.datagridTotal(model));
			} else {
				VRepertory vrepertory = new VRepertory();
				vrepertory.setUid(sessionInfo.getUserId());
				List<VRepertory> vrepertories = repertoryService.list(vrepertory);
				if (vrepertories != null && vrepertories.size() > 0) {
					List<VSalelist> rows = new ArrayList<VSalelist>();
					Long total = 0l;
					for (VRepertory vre : vrepertories) {
						model.setRid(vre.getId());
						DataGrid subDg = memberService.datagridTotal(model);
						rows.addAll(subDg.getRows());
						//total += subDg.getTotal();
					}
					DataGrid totalDg = new DataGrid();
					totalDg.setRows(rows);
					//totalDg.setTotal(total);
					totalDg.setTotal(new Long(rows.size()));
					this.writeJson(totalDg);
				} else {
					DataGrid dg = new DataGrid();
					dg.setTotal(0l);
					dg.setRows(new ArrayList<VSalelist>());
					this.writeJson(dg);
				}
			}
			
		}*/
		
	}
	
	public String saleRecord() {
		getRequest().setAttribute("serial",
				memberService.getMemberById(model.getId()).getSerial());
		return "saleRecord";
	}

	public String pointRecord() {
		getRequest().setAttribute("serial",
				memberService.getMemberById(model.getId()).getSerial());
		return "pointRecord";
	}

}
